package com.hk.aefz.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hk.aefz.po.Special;
import com.hk.aefz.service.SpecialService;
import com.hk.aefz.vo.PageResult;
import com.hk.aefz.vo.ResultMsg;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 个人分类专栏 前端控制器
 * </p>
 *
 * @author 阿尔法哲
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/special")
public class SpecialController {

    @Autowired
    private SpecialService specialService;

    @ApiOperation("新建个人分类")
    @PostMapping(path = "/saveSpecial")
    public ResultMsg saveSpecial(Special special) {
        System.err.println("请求新增的分类："+special);
        ResultMsg resultMsg = specialService.insertSpecial(special);
        return resultMsg;

    }

    @ApiOperation("修改个人单独分类")
    @PostMapping(path = "/updateSpecial")
    public ResultMsg updateSpecial(Special special) {
        ResultMsg resultMsg = specialService.updateSpecial(special);
        return resultMsg;

    }

    @ApiOperation("跳转到个人分类管理 查询")
    @GetMapping(path = "/classify-admin")
    public ModelAndView classifyAdmin(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "5") Integer size) {
        //        创建一个变量定义未删除状态
        final int dedid = 0;
        ModelAndView mv = new ModelAndView("/blogafter/adminClassify");
        PageResult<Special> pageSpecial = specialService.selectAll(page,size,dedid);
        mv.addObject("pageSpecial",pageSpecial);
        return mv;
    }

    @ApiOperation(value = "逻辑删除放入回收站/恢复")
    @GetMapping(path = "/del-special")
    public ResultMsg delSpecial(Integer sid,Integer dedid) {
        ResultMsg resultMsg = new ResultMsg();
        Special special = new Special();
        special.setDedid(dedid);
//        修改条件
        Integer ded = dedid == 1 ? 0:1;
        QueryWrapper<Special> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sid",sid)
                .eq("sstate",0)
                .eq("dedid",ded);
        boolean removeById = specialService.update(special,queryWrapper);
        if (removeById && dedid == 1) {
            resultMsg.setState(200);
            resultMsg.setMsg("已放入回收站!");
        } else if (removeById && dedid == 0) {
            resultMsg.setState(200);
            resultMsg.setMsg("已恢复!");
        }else {
            resultMsg.setState(404);
            resultMsg.setMsg("请求异常!稍后重试");
        }
        return resultMsg;
    }

    @ApiOperation("查询分类回收站")
    @GetMapping(path = "recycle-special")
    public PageResult<Special> recycleSpecialAll(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "5") Integer size) {
        //        创建一个变量定义删除状态
        final int dedid = 1;
        PageResult<Special> pageSpecial = specialService.selectAll(page,size,dedid);
        return pageSpecial;
    }

    @ApiOperation("永久删除分类")
    @GetMapping(path = "/delete-special")
    public ResultMsg deteteSpecial(Integer sid) {
        ResultMsg resultMsg = specialService.deleteSpecial(sid);
        return resultMsg;
    }

    @ApiOperation("根据分类id查询单独分类信息")
    @PostMapping(path = "/select-bysid")
    public Special selectBySid(Integer sid,Integer dedid) {
        Special special = specialService.selectBySid(sid,dedid);
        return special;
    }

}

